/*
 * @Author: your name
 * @Date: 2021-08-25 09:52:04
 * @LastEditTime: 2021-09-10 13:55:13
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \musicapp\src\store\index.js
 */
import { createStore } from 'vuex'
import { getLyric, phoneLogin, userDetail } from '@/api/index.js'
export default createStore({
    state: {
        playlist: [{
            name: "骗自己",
            id: 1854151837,
            al: {
                id: 129185462,
                name: "骗自己",
                pic: 109951166096206160,
                picUrl: "http://p4.music.126.net/-m3uvfiZbSxTfmAQBsPitQ==/109951166096206163.jpg",
                pic_str: "109951166096206163",
            }
        }],
        playCurrentIndex: 0,
        lyric: '',
        currentTime: 0,
        intervalId: 0,
        user: {
            isLogin: false,
            userDetailInfo: {

            },
            account: {

            }
        }
    },
    mutations: {
        setPlaylist: function(state, value) {
            state.playlist = value
        },
        pushPlaylist: function(state, value) {
            state.playlist.push(value)
        },
        setPlayIndex(state, value) {
            state.playCurrentIndex = value
        },
        setLyric(state, value) {
            state.lyric = value
        },
        setCurrentTime(state, value) {
            state.currentTime = value
        },
        setUser(state, value) {
            state.user = value

        },

    },
    actions: {
        async reqLyric(content, payload) {
            // console.log(payload);
            console.log(payload);
            let result = await getLyric(payload.id)
            console.log(result);
            if (result.data.lrc) {
                content.commit('setLyric', result.data.lrc.lyric)
            } else {
                content.commit('setLyric', '暂无歌词')
            }
            // console.log(result.data.lrc.lyric);
        },
        async phoneLogin(content, payload) {
            // console.log(payload);
            console.log(content);
            let result = await phoneLogin(payload.phone, payload.password)
            if (result.data.code == 200) {
                content.state.user.isLogin = true;


                content.state.user.account = result.data.account

                let userDetailInfo = await userDetail(result.data.account.id)
                console.log(userDetailInfo);
                content.state.user.userDetailInfo = userDetailInfo.data;
                localStorage.userData = JSON.stringify(content.state.user)
                content.commit('setUser', content.state.user)
            }
            return result;
            // console.log(result);
        }


    },
    getters: {
        lyricList: function(state) {
            let arr = state.lyric.split(/\n/igs).map((item, i, arr) => {
                let min = item.slice(1, 3);
                let sec = item.slice(4, 6);
                let mill = item.slice(7, 10);
                // if (i - 1 < 0) {
                //    let pre = 0
                // }else{
                //    let pre = arr[i - 1].slice(4,6)
                // }

                return {
                    min,
                    sec,
                    mill,
                    lyric: item.slice(11, item.length),
                    content: item,
                    time: parseInt(mill) + parseInt(sec) * 1000 + parseInt(min) * 60 * 1000
                }
            })
            arr.forEach((item, i) => {
                if (i - 1 < 0) {
                    item.pre = 0
                } else {
                    item.pre = arr[i - 1].time
                }

            })
            console.log(arr);
            return arr
        }
    },
    modules: {}
})